<?php
namespace app\common\loglic;

use app\common\loglic\Model;

class Captcha extends Model
{
    private $captcha = '';
    
    //return int(自增ID)
    public function save($data=[])
    {
        if(!$data['captcha_mobile']){
            return 0;
        }
        $this->captcha = rand(100000,999999);
        $data['captcha_value']        = $this->captcha;
        $data['captcha_error']        = 0;//验证次数
        $data['captcha_create_time']  = time();
        $data['captcha_create_ip']    = request()->ip();
        return $this->dbSave('captcha', $data);
    }
    
    public function delete($where=[])
    {
        return $this->dbDelete('captcha', $where);
    }
    
    public function update($where=[],$data=[])
    {
        return $this->dbUpdate('captcha', $where, $this->setData('update',$data));
    }
    
    public function select($args=[])
    {
        return $this->dbSelect('captcha', $args);
    }
    
    public function selectPage($args=[], $pageSize=10, $pageNumber=1, $isTotal=false, $query='')
    {
        return $this->dbSelectPage('captcha', $args, $pageSize, $pageNumber, $isTotal, $query);
    }
    
    //where二维数组
    public function get($args=[])
    {
        $where = [];
        foreach($args as $key=>$value){
            array_push($where,$value);
        }
        if(!$where){
            return false;
        }
        return $this->dbGet('captcha', [
            'where' => $where,
            'order' => 'captcha_id desc',
        ]);
    }
    
    public function getMobile($mobile='')
    {
        return $this->get([
            ['captcha_mobile','eq',$mobile],
        ]);
    }
    
    //发送验证码
    public function send($mobile='')
    {
        //60秒内禁止重发
        if($this->isExpire($mobile)){
            return false;
        }
        //写入数据库
        $captchaId = $this->save([
            'captcha_mobile' => $mobile
        ]);
        if(!$captchaId){
            $this->error = '创建验证码失败';
        }
        //调用阿里云验证码发送接口
        if(!$result = model('common/Alisms','loglic')->captcha($mobile,$this->captcha)){
            $this->error = model('common/Alisms','loglic')->getError();
            return false;
        }
        return true;
    }
    
    //判断是否在60秒保护期期内（防止多次重发） return true|false
    public function isExpire($mobile='')
    {
        $result = $this->getMobile($mobile);
        if(!$result){
            return false;
        }
        if($result['captcha_create_time']+60 < time()){
            return false;
        }
        $this->error = '请稍等60秒';
        return true;
    }
    
    //return true|false
    //验证用户输入的验证码是否与数据库的一致
    public function check($post=[])
    {
        if(!$post['user_mobile'] || !$post['user_captcha']){
            return false;
        }
        //获取验证码
        if(!$result = $this->getMobile($post['user_mobile'])){
            $this->error = '请先获取验证码';
            return false;
        }
        //错误太多或已失效
        if($result['captcha_error'] > 19){
            $this->error = '验证码已失效，请重新获取';
            return false;
        }
        //已超时（10分钟内有效）
        if($result['captcha_create_time']+600 < time()){
            $this->error = '验证码超时，请重新获取';
            $this->errorInc($result['captcha_id'],1);
            return false;
        }
        //验证是否相等
        if($result['captcha_value'] != $post['user_captcha']){
            $this->error = '验证码错误，请重新输入';
            $this->errorInc($result['captcha_id'],1);
            return false;
        }
        //验证成功
        $this->errorInc($result['captcha_id'],20);
        //返回结果
        return true;
    }
    
    //递增错误值
    private function errorInc($captchaId=0,$step=1)
    {
        return db('captcha')->where('captcha_id', $captchaId)->setInc('captcha_error',$step);
    }
}